﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FolderRenameActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property OriginalFolderName As InArgument(Of String)
    Public Property NewFolderName As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncFolderRenameDelegate(ByVal original As String, ByVal target As String) As Boolean


    Public Sub New()
        Me.DisplayName = My.Resources.FolderRenameDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim asyncExecute = New asyncFolderRenameDelegate(AddressOf FolderRename)
        Dim original = context.GetValue(Me.OriginalFolderName)
        Dim newname = context.GetValue(Me.NewFolderName)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(original, newname, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncFolderRenameDelegate)
        Dim renameResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, renameResult)
    End Sub

    Private Function FolderRename(ByVal original As String, ByVal target As String) As Boolean
        Dim renameResult = False
        Try
            If Not System.IO.Directory.Exists(original) Then Exit Try '対象フォルダが存在しない場合はエラー
            If System.IO.Directory.Exists(target) Then Exit Try '新名称で既に存在はエラー

            My.Computer.FileSystem.RenameDirectory(original, target)
            renameResult = True
        Catch ex As Exception

        End Try
        Return renameResult
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FolderRename Activity
        build.AddCustomAttributes(GetType(FolderRenameActivity),
                                              New ToolboxBitmapAttribute(GetType(FolderRenameActivity), "FolderRename.png"))
        build.AddCustomAttributes(GetType(FolderRenameActivity), "OriginalFolderName",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderRenameCategory),
                                              New DisplayNameAttribute(My.Resources.FolderRenameOriginalFolderNameDisplay),
                                              New DescriptionAttribute(My.Resources.FolderRenameOriginalFolderNameDescription))
        build.AddCustomAttributes(GetType(FolderRenameActivity), "NewFolderName",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderRenameCategory),
                                              New DisplayNameAttribute(My.Resources.FolderRenameNewFolderNameDisplay),
                                              New DescriptionAttribute(My.Resources.FolderRenameNewFolderNameDescription))
        build.AddCustomAttributes(GetType(FolderRenameActivity), "Result",
                                              New CategoryAttribute(My.Resources.FolderRenameCategory),
                                              New DisplayNameAttribute(My.Resources.FolderRenameResultDisplay),
                                              New DescriptionAttribute(My.Resources.FolderRenameResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)

    End Sub
End Class
